//
//  BaseTabBarController.m
//  BakeCenter
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 Baked. All rights reserved.
//

#import "BaseTabBarController.h"
@interface BaseTabBarController ()<UITabBarControllerDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong)UIButton *button;

@end

@implementation BaseTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    HomePageTableViewController *homePageTVC = [[HomePageTableViewController alloc] initWithStyle:(UITableViewStyleGrouped)];

    ClassificationViewController *classificationVC = [[ClassificationViewController alloc] init];
    
    ShoppingViewController *shoppingVC = [[ShoppingViewController alloc] init];
    
    MineTableViewController *mineVC = [[MineTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
    [self addChildVC:homePageTVC title:@"首页" imageName:@"Entypo_2302(4)_32" selectedImage:@""];
    [self addChildVC:classificationVC title:@"分类" imageName:@"Entypo_268f(1)_32" selectedImage:@""];
    [self addChildVC:nil title:@"" imageName:@"" selectedImage:@""];
    [self addChildVC:shoppingVC title:@"淘工具" imageName:@"Entypo_e73d(0)_32" selectedImage:@""];
    [self addChildVC:mineVC title:@"我的" imageName:@"Entypo_f300(3)_32" selectedImage:@""];
    
    [self setup];
    
    self.tabBar.tintColor = [UIColor cyanColor];
   
}


#pragma mark- setup
-(void)setup
{
    //  添加突出按钮
    [self addCenterButtonWithImage:[UIImage imageNamed:@"111.jpg"] selectedImage:[UIImage imageNamed:@"222.jpg"]];
    self.button.layer.masksToBounds = YES;
//    self.button.layer.borderWidth = 2;
    
    self.button.layer.borderColor = [[UIColor blackColor] CGColor];
    self.button.layer.cornerRadius = 30;
    //  UITabBarControllerDelegate 指定为自己
    self.delegate=self;
//    //  指定当前页——中间页
//    self.selectedIndex=2;
//    //  设点button状态
//    self.button.selected=YES;

}


#pragma mark - addCenterButton
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage selectedImage:(UIImage*)selectedImage
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button addTarget:self action:@selector(pressChange:) forControlEvents:UIControlEventTouchUpInside];
    self.button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    
    //  设定button大小为适应图片
    self.button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [self.button setImage:buttonImage forState:UIControlStateNormal];
    [self.button setImage:selectedImage forState:UIControlStateSelected];
    
    //  这个比较恶心  去掉选中button时候的阴影
    self.button.adjustsImageWhenHighlighted=NO;
    
    
    /*
     *  核心代码：设置button的center 和 tabBar的 center 做对齐操作， 同时做出相对的上浮
     */
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        self.button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        self.button.center = center;
    }
    
    [self.view addSubview:self.button];
}

-(void)pressChange:(id)sender
{
    self.button.selected = !self.button.selected;
//    self.selectedIndex=2;
//    self.button.selected=YES;
    if (self.button.selected == YES) {
        [self setActionSheet];
        self.button.selected = NO;
    }
}


//添加 actionSheet
-(void)setActionSheet{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册中选择", nil];
    [actionSheet showFromTabBar:self.tabBar];
    
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
//        [self addCarema];
        ShoppingViewController *vvv = [[ShoppingViewController alloc] init];
        [self presentViewController:vvv animated:YES completion:nil];
    }
    if (buttonIndex == 1) {
        [self openPicLibrary];
    }

}

-(void)addCarema
{
    //判断是否可以打开相机,模拟器无法使用此功能
    if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES; //是否可编辑
        //摄像头
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    }else
    {
        //如果没有提示用户
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"无法检测到摄像头" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

//拍摄完成后要执行的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //得到图片
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //图片存入相册
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
//点击Cancel按钮后执行方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)openPicLibrary
{
    //相册是可以用模拟器打开的
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        
        //打开相册选择照片
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
}



//#pragma mark- TabBar Delegate
//
////  换页和button的状态关联上
//
//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//    if (self.selectedIndex==2) {
//        self.button.selected=YES;
//    }else
//    {
//        self.button.selected=NO;
//    }
//}






#pragma mark -------------添加tabBar和NavigationContrller---------------

-(void)addChildVC:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImage
{
    //设置默认图片
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];

    
    //设置选中图片
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    
    
    //设置导航控制器
    UINavigationController *childNVC = [[UINavigationController alloc] initWithRootViewController:childVC];
    childVC.navigationItem.title = title;
    childNVC.tabBarItem.title = title;
//    childNVC.navigationController.navigationBar.translucent = YES;
    childVC.navigationController.navigationBar.translucent = NO;
    self.tabBar.translucent = NO;
    childVC.navigationController.navigationBar.translucent = NO;
    self.tabBar.translucent = NO;

    
    //将设置好的导航控制器添加到TabbarController
    [self addChildViewController:childNVC];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
